package jnpf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.entity.ZXGL_RenWuFanKuiJiaoFuWuEntity;
import jnpf.entity.ZXGL_RenWuFanKuiXieBanEntity;
import jnpf.entity.ZXGL_XieBanRenWuFanKuiMXEntity;
import jnpf.mapper.ZXGL_XieBanRenWuFanKuiMXMapper;
import jnpf.service.ZXGL_RenWuFanKuiJiaoFuWuService;
import jnpf.service.ZXGL_RenWuFanKuiService;
import jnpf.service.ZXGL_RenWuFanKuiXieBanService;
import jnpf.service.ZXGL_XieBanRenWuFanKuiMXService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;
import java.math.BigDecimal;








import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.text.SimpleDateFormat;
import jnpf.util.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * ZXGL_XieBanRenWuFanKuiMX
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-20
 */
@Service
public class ZXGL_XieBanRenWuFanKuiMXServiceImpl extends ServiceImpl<ZXGL_XieBanRenWuFanKuiMXMapper, ZXGL_XieBanRenWuFanKuiMXEntity> implements ZXGL_XieBanRenWuFanKuiMXService  {

    private ZXGL_RenWuFanKuiXieBanService zxglRenWuFanKuiXieBanService;
    private ZXGL_RenWuFanKuiJiaoFuWuService zxglRenWuFanKuiJiaoFuWuService;
    private ZXGL_RenWuFanKuiService zxglRenWuFanKuiService;

    /***
     * 协办人流程走完后修改协办反馈子表
     * @param id
     * @return
     */
    @Override
    public Boolean updateXieBanMX(String id) {
        //获取子流程卡
        ZXGL_RenWuFanKuiXieBanEntity byId = zxglRenWuFanKuiXieBanService.getById(id);
        QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_RenWuFanKuiJiaoFuWuEntity::getMainid, id);
        ZXGL_RenWuFanKuiJiaoFuWuEntity jfw = zxglRenWuFanKuiJiaoFuWuService.getOne(queryWrapper);
        ZXGL_XieBanRenWuFanKuiMXEntity xieBanRenWuFanKuiMX = new ZXGL_XieBanRenWuFanKuiMXEntity();
        //更新协办反馈子表
        xieBanRenWuFanKuiMX.setMainid(id);
        xieBanRenWuFanKuiMX.setMainid(byId.getZlcid());
        xieBanRenWuFanKuiMX.setFkr(byId.getFkr());
        xieBanRenWuFanKuiMX.setFkzt("已反馈");
        xieBanRenWuFanKuiMX.setJfw(jfw.getJfw());
        QueryWrapper<ZXGL_XieBanRenWuFanKuiMXEntity> query = new QueryWrapper<>();
        query.lambda().eq(ZXGL_XieBanRenWuFanKuiMXEntity::getMainid,id).eq(ZXGL_XieBanRenWuFanKuiMXEntity::getFkr,byId.getFkr());
        int update = baseMapper.update(xieBanRenWuFanKuiMX, query);
        if (update==1){
            //查询子流程是否有全部反馈
            QueryWrapper<ZXGL_XieBanRenWuFanKuiMXEntity> renwu = new QueryWrapper<>();
            renwu.lambda().eq(ZXGL_XieBanRenWuFanKuiMXEntity::getMainid,byId.getMainid());
            List<ZXGL_XieBanRenWuFanKuiMXEntity> zxglXieBanRenWuFanKuiMXEntities = baseMapper.selectList(renwu);
            List<ZXGL_XieBanRenWuFanKuiMXEntity> fankui = zxglXieBanRenWuFanKuiMXEntities.stream().filter(e -> e.getFkzt().equals("未反馈")).collect(Collectors.toList());
            if (fankui!=null && fankui.size()==0){
                //修改任务反馈主流程反馈状态
                zxglRenWuFanKuiService.updateState(id);
            }
        }
        return true;
    }
}